const { Op, literal } = require("sequelize");
const Video = require("../models/Video.js");

class VideoService {
    async addVideo(videoData) {
        return Video.create(videoData);
    }
    async getVideoById(id) {
        return Video.findByPk(id);
    }
    async getAllVideos() {
        return Video.findAll({
            where: {
                status: 'enabled'
            }
        });
    }
    async updateVideo(videoData) {
        const videoInstance = await Video.findByPk(videoData.id);
        videoInstance.update(videoData);
    }
    async deleteVideo(videoId) {
        return Video.destroy({
            where: {
                id: videoId
            }
        });
    }
    async getVideoListByPage(page = 1, limit = 10, keyword = undefined, status, sort = 'DESC') {
        let where = {
            deleted_at: null
        }
    
        if (status) {
            where.status = status
        }
        return Video.findAndCountAll({
            limit,
            offset: (page - 1) * limit,
            where,
            order: [['id', sort]],
            distinct: true,
            attributes: [
                'id',
                'filename',
                'status',
                [literal('DATE_FORMAT(Video.created_at, "%Y-%m-%d %H:%i:%s")'), 'created_at']
            ]
        });
    }
}

module.exports = new VideoService();